home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / sipp / srgp / src / srgp_inp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-01  |  13.8 KB  |  597 lines

  1. #include "HEADERS.h"
  2. #include "srgplocal.h"
  3.  
  4. #ifdef RS6000
  5. #define NBBY 8
  6. #include <sys/time.h>
  7. #include <sys/select.h>
  8. #elif UNIX
  9. #include <sys/types.h>
  10. #include <sys/time.h>
  11. #endif
  12.  
  13. #ifdef GRX
  14. #   include <time.h>
  15. #   ifdef __GNUC__
  16. #    define clock()  rawclock()
  17. #   endif
  18. #endif
  19.  
  20. /** DEVICE INDEPENDENCE
  21. This file contains functions that are device-independent,
  22.    except for the "sleeping" arrangements for SRGP_waitEvent.
  23. Refer to inputraw.c and echo.c for the low-level "drivers"
  24.    for each type of implementation.
  25.  
  26. **/
  27.  
  28.  
  29. static void
  30. ComputeTimestamp (srgp_timestamp *ts)
  31. {
  32.    Time tdiff =  srgpx__cur_time - srgpx__starttime;
  33.  
  34. #ifdef GRX
  35.    ts->seconds = (tdiff * 10) / 182;
  36. /* ts->ticks   = (((tdiff * 10) % 182) * 60) / 182;    */
  37. /* THIS WOULD BE REALLY ACCURATE, BUT I CHEAT A LITTLE: */
  38.    ts->ticks   = ((tdiff * 10) % 182) / 3;
  39. #else
  40.    ts->seconds = tdiff / rawgranularity;
  41.    ts->ticks = (tdiff % rawgranularity) / ((double)rawgranularity) * 60;
  42. #endif
  43. }
  44.  
  45.  
  46.  
  47. /** SRGP __ initInputModule **/
  48. void
  49. SRGP__initInputModule ()
  50. {
  51.    /* DEFAULT KEYBOARD ECHO POSITION IS MIDDLE OF SCREEN WINDOW. */
  52.    srgp__cur_keyboard_echo_origin =
  53.       SRGP_defPoint(srgp__canvasTable[0].max_xcoord >> 1,
  54.             srgp__canvasTable[0].max_ycoord >> 1);
  55.    srgp__cur_locator_echo_anchor = srgp__cur_keyboard_echo_origin;
  56.  
  57.    /* DEFAULT KEYBOARD ATTRIBUTES */
  58.    srgp__cur_keyboard_echo_font = 0;
  59.    srgp__cur_keyboard_echo_color = SRGP_BLACK;
  60.  
  61.    SRGP__disableLocatorCursorEcho();
  62.  
  63.    /* INITIALIZE CURSOR TABLE. */
  64.    PUSH_TRACE;
  65.    /* SRGP_loadCursor (0, XC_arrow);    "device" dependent!!! */
  66.    POP_TRACE;
  67.    srgp__cur_cursor = 0;
  68.  
  69.    /* INITIALIZE ACTIVITY INFORMATION. */
  70.    srgp__cur_mode[LOCATOR]=srgp__cur_mode[KEYBOARD]=INACTIVE;
  71.    srgp__cur_locator_echo_type = CURSOR;
  72.    srgp__cur_keyboard_processing_mode = EDIT;
  73.  
  74.    /* INITIALIZE MEASURES AND MASKS */
  75.    srgp__cur_locator_measure.button_chord[LEFT_BUTTON] = UP;
  76.    srgp__cur_locator_measure.button_chord[MIDDLE_BUTTON] = UP;
  77.    srgp__cur_locator_measure.button_chord[RIGHT_BUTTON] = UP;
  78.    srgp__cur_locator_measure.position = srgp__cur_locator_echo_anchor;
  79.  
  80.    srgp__cur_keyboard_measure.buffer = malloc(MAX_STRING_SIZE+1);
  81.    srgp__cur_keyboard_measure.buffer_length = MAX_STRING_SIZE+1;
  82.    srgp__cur_keyboard_measure.buffer[0] = '\0';
  83.    srgp__get_keyboard_measure.buffer = malloc(MAX_STRING_SIZE+1);
  84.    srgp__get_keyboard_measure.buffer_length = MAX_STRING_SIZE+1;
  85.    srgp__get_keyboard_measure.buffer[0] = '\0';
  86.  
  87.    srgp__cur_locator_button_mask = LEFT_BUTTON_MASK;
  88.  
  89.    SRGP__initInputDrivers ();
  90.    SRGP__initEchoModule ();
  91. }
  92.  
  93.  
  94.  
  95.  
  96. /** SRGP set input mode
  97. The device is first deactivated, then its cur_mode status is updated,
  98. then it is activated
  99. **/
  100. void
  101. SRGP_setInputMode (inputDevice device, inputMode mode)
  102. {
  103.    DEBUG_AIDS{
  104.       SRGP_trace (SRGP_logStream, "SRGP_setInputMode  %d into %d\n",
  105.           device, mode);
  106.       srgp_check_system_state();
  107.       srgp_check_device (device);
  108.       srgp_check_mode (mode);
  109.       LeaveIfNonFatalErr();
  110.    }
  111.  
  112.    if (mode == srgp__cur_mode[device])
  113.       /* NOTHING IS TO BE DONE. */
  114.       return;
  115.  
  116.    if (mode == INACTIVE) {
  117.       /* WE ARE BRINGING AN ACTIVE DEVICE TO INACTIVITY. */
  118.       SRGP__deactivateDevice (device);
  119.       srgp__cur_mode[device] = INACTIVE;
  120.    }
  121.  
  122.    else {
  123.        /*
  124.       if ((device==LOCATOR)&&(srgp__cur_mode[LOCATOR]==INACTIVE))
  125.      SRGP__updateRawCursorPosition();
  126.      */
  127.       srgp__cur_mode[device] = mode;
  128.       SRGP__activateDevice (device);
  129.    }
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. /** SRGP wait event
  138. The event in the queue which satisfies the wait is removed
  139. and placed at the tail of the free-list.  It is there that
  140. the "get" routines look.
  141.  
  142. Returns the identifier of the device which issued the event.
  143.    (NO_DEVICE if none).
  144. **/
  145.  
  146. inputDevice
  147. SRGP_waitEvent (maximum_wait_time)
  148. int maximum_wait_time;
  149. {
  150. #ifdef X11
  151.    struct timeval tp, expiretime, timeout, *timeout_ptr;
  152.    struct timezone tzp;
  153.    int fd;
  154.    fd_set readfds;
  155. #endif
  156. #ifdef THINK_C
  157.    Time curtime, expiretime=0;
  158. #endif
  159. #ifdef GRX
  160.    long expiretime;
  161. #endif
  162.    int return_value;
  163.    boolean do_continue_wait, do_continue_search;
  164.    boolean forever = (maximum_wait_time < 0);
  165.  
  166.    DEBUG_AIDS{
  167.       srgp_check_system_state();
  168.       LeaveIfNonFatalErr();
  169.    }
  170.  
  171.  
  172. #ifdef UNIX
  173. #define MILLION 1000000
  174.    FD_ZERO (&readfds);
  175.    FD_SET (fd=ConnectionNumber(srgpx__display), &readfds);
  176.  
  177.    /* INITIALIZE (if necessary) TIMING INFORMATION */
  178.    if (maximum_wait_time > 0) {
  179.       long maxwait_sec, maxwait_ticks;
  180.       gettimeofday (&tp,&tzp);
  181.       maxwait_sec = maximum_wait_time / 60;
  182.       maxwait_ticks = maximum_wait_time % 60;
  183.       expiretime.tv_usec = tp.tv_usec + (maxwait_ticks*MILLION/60);
  184.       expiretime.tv_sec = tp.tv_sec + maxwait_sec +
  185.               (expiretime.tv_usec / MILLION);
  186.       expiretime.tv_usec %= MILLION;
  187.    }
  188. #endif
  189.  
  190. #ifdef THINK_C
  191.    expiretime = TickCount() + maximum_wait_time;
  192. #endif
  193.  
  194. #ifdef GRX
  195.    if(maximum_wait_time > 0) {
  196.     expiretime = clock() + ((maximum_wait_time * 182) / 600);
  197.    }
  198. #endif
  199.  
  200.    do_continue_wait = TRUE;
  201.  
  202.    return_value = NO_DEVICE;  /* timeout is what we assume will happen */
  203.  
  204.  
  205.    /**** LOOP *****/
  206.    do {
  207.       return_value = SRGP__handleRawEvents (TRUE,forever);
  208.  
  209.       if ((return_value != NO_DEVICE) || (maximum_wait_time == 0))
  210.      do_continue_wait = FALSE;
  211.  
  212. #ifdef UNIX
  213.       /* Otherwise, perform a wait state */
  214.       else {
  215.      if (!forever) {
  216.         gettimeofday (&tp,&tzp);
  217.         /* Perform subtraction: expiretime - tp */
  218.         if (expiretime.tv_usec < tp.tv_usec) {
  219.            /* perform a borrow */
  220.            expiretime.tv_sec--;
  221.            expiretime.tv_usec += 1000000;
  222.         }
  223.         timeout_ptr = &timeout;
  224.         timeout.tv_usec = expiretime.tv_usec - tp.tv_usec;
  225.         timeout.tv_sec  = expiretime.tv_sec  - tp.tv_sec;
  226.         if ((timeout.tv_sec < 0) || (timeout.tv_usec < 0))
  227.            /* Whoops!  We've already expired! */
  228.            do_continue_wait = FALSE;
  229.      }
  230.      else /* (maximum_wait_time is negative representing infinity) */
  231.         timeout_ptr = NULL;
  232.  
  233.      if (do_continue_wait)
  234.         if (XPending(srgpx__display) == 0) {
  235.            if (select (fd+1, &readfds, NULL, NULL, timeout_ptr))
  236.           /* An event occurred before the timeout! */
  237.           ;  /* so do nothing */
  238.            else
  239.           do_continue_wait = FALSE;
  240.         }
  241.       }
  242. #endif
  243.  
  244. #ifdef THINK_C
  245.       else
  246.      if (maximum_wait_time > 0) {
  247.         do_continue_wait =  (TickCount() < expiretime);
  248.      }
  249. #endif
  250.  
  251. #ifdef GRX
  252.       else if(maximum_wait_time > 0) {
  253.         do_continue_wait =  (clock() < expiretime);
  254.       }
  255. #endif
  256.    }
  257.    while (do_continue_wait);
  258.  
  259.    srgp__device_at_head_of_queue = return_value;
  260.  
  261.    return return_value;
  262. }
  263.  
  264.  
  265.  
  266. void
  267. SRGP_getLocator (srgp__locator_measure *measure)
  268. {
  269.    DEBUG_AIDS{
  270.       srgp_check_system_state();
  271.       srgp_check_event_type (LOCATOR);
  272.       LeaveIfNonFatalErr();
  273.    }
  274.  
  275.    /* this assignment statement is very risky!  God help me! */
  276.    *measure = *((srgp__locator_measure*)(&srgp__get_locator_measure));
  277. }
  278.  
  279.  
  280. void
  281. SRGP_getDeluxeLocator (srgp__deluxe_locator_measure *measure)
  282. {
  283.    DEBUG_AIDS{
  284.       srgp_check_system_state();
  285.       srgp_check_event_type (LOCATOR);
  286.       LeaveIfNonFatalErr();
  287.    }
  288.  
  289.    *measure = srgp__get_locator_measure;
  290.    ComputeTimestamp (&(measure->timestamp));
  291. }
  292.  
  293.  
  294.  
  295. void
  296. SRGP_getKeyboard (char *measure, int bufsize)
  297. {
  298.    DEBUG_AIDS{
  299.       srgp_check_system_state();
  300.       srgp_check_event_type (KEYBOARD);
  301.       LeaveIfNonFatalErr();
  302.    }
  303.    strncpy (measure, srgp__get_keyboard_measure.buffer, bufsize-1);
  304.    *(measure+bufsize-1) = '\0';
  305. }
  306.  
  307.  
  308. void
  309. SRGP_getDeluxeKeyboard (srgp__deluxe_keyboard_measure *measure)
  310. {
  311.    DEBUG_AIDS{
  312.       srgp_check_system_state();
  313.       srgp_check_event_type (KEYBOARD);
  314.       LeaveIfNonFatalErr();
  315.    }
  316.    strncpy (measure->buffer, srgp__get_keyboard_measure.buffer,
  317.         measure->buffer_length-1);
  318.    *(measure->buffer+measure->buffer_length-1) = '\0';
  319.    bcopy (srgp__get_keyboard_measure.modifier_chord,
  320.       measure->modifier_chord,
  321.       sizeof(measure->modifier_chord));
  322.    measure->position = srgp__get_keyboard_measure.position;
  323.  
  324.    ComputeTimestamp (&(measure->timestamp));
  325. }
  326.  
  327.  
  328.  
  329.  
  330. /** MEASURE SETTING **/
  331.  
  332. void
  333. SRGP_setLocatorMeasure (point position)
  334. {
  335.    DEBUG_AIDS{
  336.       SRGP_trace (SRGP_logStream, "SRGP_setLocatorMeasure %d,%d\n",
  337.           position.x, position.y);
  338.       srgp_check_system_state();
  339.       LeaveIfNonFatalErr();
  340.    }
  341.  
  342.    SRGP__handleRawEvents (FALSE,FALSE);
  343.  
  344.    srgp__cur_locator_measure.position = position;
  345.  
  346.    SRGP__updateRawCursorPosition ();
  347. }
  348.  
  349.  
  350. /*!*/
  351. void
  352. SRGP_setKeyboardMeasure (str)
  353. char *str;
  354. {
  355.    DEBUG_AIDS{
  356.       SRGP_trace (SRGP_logStream, "SRGP_setKeyboardMeasure %s\n", str);
  357.       srgp_check_system_state();
  358.       LeaveIfNonFatalErr();
  359.    }
  360.    SRGP__handleRawEvents (FALSE,FALSE);
  361.  
  362.    strncpy (srgp__cur_keyboard_measure.buffer, str, MAX_STRING_SIZE);
  363.    *(srgp__cur_keyboard_measure.buffer+MAX_STRING_SIZE) = '\0';
  364.    srgp__cur_keyboard_measure_length = strlen(srgp__cur_keyboard_measure.buffer);
  365.  
  366.    SRGP__updateKeyboardEcho ();
  367. }
  368.  
  369.  
  370.  
  371.  
  372. /** ATTRIBUTES
  373. The routines allow the application to control the echoing
  374. associated with the keyboard and the locator devices.
  375. **/
  376.  
  377. /*!*/
  378.  
  379. void
  380. SRGP_setLocatorEchoType (echoType value)
  381. {
  382.    DEBUG_AIDS{
  383.       SRGP_trace (SRGP_logStream, "SRGP_setLocatorEchoType %d\n", value);
  384.       srgp_check_system_state();
  385.       srgp_check_locator_echo_type(value);
  386.       LeaveIfNonFatalErr();
  387.    }
  388.  
  389.    SRGP__handleRawEvents (FALSE,FALSE);
  390.  
  391.    SRGP__disableLocatorRubberEcho();
  392.    SRGP__disableLocatorCursorEcho();
  393.  
  394.    srgp__cur_locator_echo_type = value;
  395.  
  396.    SRGP__enableLocatorRubberEcho();
  397.    SRGP__enableLocatorCursorEcho();
  398.  
  399.    SRGP__updateInputSelectionMask();
  400. }
  401.  
  402.  
  403. /*!*/
  404.  
  405. void
  406. SRGP_setLocatorEchoCursorShape (int id)
  407. {
  408.    DEBUG_AIDS{
  409.       SRGP_trace (SRGP_logStream, "SRGP_setLocatorEchoCursorShape %d\n", id);
  410.       srgp_check_system_state();
  411.       LeaveIfNonFatalErr();
  412.    }
  413.    SRGP__handleRawEvents (FALSE,FALSE);
  414.  
  415.    srgp__cur_cursor = id;
  416.  
  417.    SRGP__updateLocatorCursorShape ();
  418. }
  419.  
  420.  
  421. /*!*/
  422.  
  423. void
  424. SRGP_setLocatorEchoRubberAnchor (point position)
  425. {
  426.    DEBUG_AIDS{
  427.       SRGP_trace (SRGP_logStream, "SRGP_setLocatorEchoRubberAnchor %d,%d\n",
  428.           position.x, position.y);
  429.       srgp_check_system_state();
  430.       LeaveIfNonFatalErr();
  431.    }
  432.  
  433.    SRGP__handleRawEvents (FALSE,FALSE);
  434.  
  435.    srgp__cur_locator_echo_anchor = position;
  436.  
  437.    SRGP__updateLocatorRubberAnchor ();
  438. }
  439.  
  440.  
  441. /*!*/
  442.  
  443.  
  444. void
  445. SRGP_setLocatorButtonMask (int value)
  446. {
  447.    DEBUG_AIDS{
  448.       SRGP_trace (SRGP_logStream, "SRGP_setLocatorButtonMask %d\n", value);
  449.    }
  450.  
  451.    srgp__cur_locator_button_mask = value;
  452. }
  453.  
  454.  
  455. /*!*/
  456.  
  457.  
  458. void
  459. SRGP_setKeyboardProcessingMode (keyboardMode value)
  460. {
  461.    DEBUG_AIDS{
  462.       SRGP_trace (SRGP_logStream, "SRGP_setKeyboardProcessingMode %d\n",
  463.           value);
  464.       srgp_check_system_state();
  465.       LeaveIfNonFatalErr();
  466.    }
  467.    SRGP__handleRawEvents (FALSE,FALSE);
  468.  
  469.    if (srgp__cur_mode[KEYBOARD] != INACTIVE)
  470.       SRGP__deactivateDevice (KEYBOARD);
  471.  
  472.    srgp__cur_keyboard_processing_mode = value;
  473.  
  474.    if (srgp__cur_mode[KEYBOARD] != INACTIVE)
  475.       SRGP__activateDevice (KEYBOARD);
  476. }
  477.  
  478.  
  479. /*!*/
  480.  
  481.  
  482. void
  483. SRGP_setKeyboardEchoColor (int value)
  484. {
  485.    DEBUG_AIDS{
  486.       SRGP_trace (SRGP_logStream, "SRGP_setKeyboardEchoColor %d\n", value);
  487.       srgp_check_system_state();
  488.       LeaveIfNonFatalErr();
  489.    }
  490.    SRGP__handleRawEvents (FALSE,FALSE);
  491.  
  492.    srgp__cur_keyboard_echo_color = value;
  493.    SRGP__updateKeyboardEchoAttributes();
  494. }
  495.  
  496.  
  497. /*!*/
  498.  
  499.  
  500. void
  501. SRGP_setKeyboardEchoOrigin (point position)
  502. {
  503.    DEBUG_AIDS{
  504.       SRGP_trace (SRGP_logStream, "SRGP_setKeyboardEchoOrigin %d,%d\n",
  505.           position.x, position.y);
  506.       srgp_check_system_state();
  507.       LeaveIfNonFatalErr();
  508.    }
  509.    SRGP__handleRawEvents (FALSE,FALSE);
  510.  
  511.    srgp__cur_keyboard_echo_origin = position;
  512.    SRGP__updateKeyboardEchoAttributes();
  513. }
  514.  
  515.  
  516. /*!*/
  517.  
  518.  
  519. void
  520. SRGP_setKeyboardEchoFont (int fontindex)
  521. {
  522.    DEBUG_AIDS{
  523.       SRGP_trace (SRGP_logStream, "SRGP_setKeyboardEchoFont %d\n", fontindex);
  524.       srgp_check_system_state();
  525.       LeaveIfNonFatalErr();
  526.    }
  527.    SRGP__handleRawEvents (FALSE,FALSE);
  528.  
  529.    srgp__cur_keyboard_echo_font = fontindex;
  530.    SRGP__updateKeyboardEchoAttributes ();
  531. }
  532.  
  533.  
  534.  
  535.  
  536. /** SAMPLERS **/
  537.  
  538. void
  539. SRGP_sampleLocator (srgp__locator_measure *measure)
  540. {
  541.    DEBUG_AIDS{
  542.       srgp_check_system_state();
  543.       LeaveIfNonFatalErr();
  544.    }
  545.    SRGP__handleRawEvents (FALSE,FALSE);
  546.    if (srgp__dirty_location)
  547.       SRGP__updateLocationKnowledge();
  548.    *measure = *((srgp__locator_measure*)(&srgp__cur_locator_measure));
  549. }
  550.  
  551.  
  552. void
  553. SRGP_sampleDeluxeLocator (srgp__deluxe_locator_measure *measure)
  554. {
  555.    DEBUG_AIDS{
  556.       srgp_check_system_state();
  557.       LeaveIfNonFatalErr();
  558.    }
  559.    SRGP__handleRawEvents (FALSE,FALSE);
  560.    if (srgp__dirty_location)
  561.       SRGP__updateLocationKnowledge();
  562.    *measure = srgp__cur_locator_measure;
  563.    ComputeTimestamp (&(measure->timestamp));
  564. }
  565.  
  566. /*!*/
  567.  
  568. void
  569. SRGP_sampleKeyboard (char *measure, int bufsize)
  570. {
  571.    DEBUG_AIDS{
  572.       srgp_check_system_state();
  573.       LeaveIfNonFatalErr();
  574.    }
  575.    SRGP__handleRawEvents (FALSE,FALSE);
  576.    strncpy (measure, srgp__cur_keyboard_measure.buffer, bufsize-1);
  577.    *(measure+bufsize-1) = '\0';
  578. }
  579.  
  580. void
  581. SRGP_sampleDeluxeKeyboard (srgp__deluxe_keyboard_measure *measure)
  582. {
  583.    DEBUG_AIDS{
  584.       srgp_check_system_state();
  585.       LeaveIfNonFatalErr();
  586.    }
  587.    SRGP__handleRawEvents (FALSE,FALSE);
  588.    strncpy (measure->buffer, srgp__cur_keyboard_measure.buffer,
  589.         measure->buffer_length-1);
  590.    *(measure->buffer+measure->buffer_length-1) = '\0';
  591.    bcopy (srgp__cur_keyboard_measure.modifier_chord,
  592.       measure->modifier_chord,
  593.       sizeof(measure->modifier_chord));
  594.    measure->position = srgp__get_keyboard_measure.position;
  595.    ComputeTimestamp (&(measure->timestamp));
  596. }
  597.